package com.quzi.kettle.activitise.process;

import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.httprequest.AsyncHttpUtilClient;
import com.loopj.android.http.RequestParams;
import com.quzi.kettle.MyApplication;
import com.quzi.kettle.R;
import com.quzi.kettle.activitise.BaseActivity;
import com.quzi.kettle.api.response.BooleanResponse;
import com.quzi.kettle.utils.StrPattern;
import com.quzi.kettle.view.ImageEditText;
import com.quzi.kettle.view.TitleBarView;

public class RegisterActivity extends BaseActivity {

	TitleBarView titleView;
	ImageEditText username, phone_number, authcode, password,
			iet_register_confirmpswd;
	Button getAuth;
	Button register;
	Handler handler;
	private int mCount = 60;
	private boolean hasSend = false;

	private String phoneNumber, code, passWord, confirmpswd, userName;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_register);

		username = (ImageEditText) findViewById(R.id.username);
		phone_number = (ImageEditText) findViewById(R.id.phonenumber);
		authcode = (ImageEditText) findViewById(R.id.authcode);
		password = (ImageEditText) findViewById(R.id.password);
		iet_register_confirmpswd = (ImageEditText) findViewById(R.id.iet_register_confirmpswd);

		getAuth = (Button) findViewById(R.id.getauth);
		getAuth.setOnClickListener(this);

		register = (Button) findViewById(R.id.register);
		register.setOnClickListener(this);

		username.update_image(R.drawable.username);
		phone_number.update_image(R.drawable.mobile);
		authcode.update_image(R.drawable.verifycode);
		password.update_image(R.drawable.passcode);
		iet_register_confirmpswd.update_image(R.drawable.passcode);

		username.setHint(getResources().getString(R.string.username));
		phone_number.setHint(getResources().getString(R.string.phone_number));
		authcode.setHint(getResources().getString(R.string.auth_code));
		password.setHint(getResources().getString(R.string.password));
		iet_register_confirmpswd.setHint(getResources().getString(
				R.string.confirmpassword));
		initTitleBar();
		handler = new Handler() {
			@Override
			public void dispatchMessage(Message msg) {
				// TODO Auto-generated method stub
				super.dispatchMessage(msg);
				switch (msg.what) {
				case 0:
					if (mCount < 0) {
						mCount = 60;
						getAuth.setText("获取验证码");
						handler.removeMessages(0);
						return;
					}
					getAuth.setText(mCount + "");
					mCount--;
					handler.sendEmptyMessageDelayed(0, 1000);
					break;
				case 1:
					dissLoadingDialog();
					break;
				}
			}
		};
	}

	@Override
	protected void initTitleBar() {
		titleView = (TitleBarView) findViewById(R.id.titleview);
		titleView.setTitle(getResources().getString(R.string.register));
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub

		switch (view.getId()) {
		case R.id.getauth:
			// 获取验证码
			if (hasSend) {
				return;
			}
			showLoadingDialog();
			phoneNumber = phone_number.getText().trim();
			String telRegex = "[1][3578]\\d{9}";
			if (!phoneNumber.isEmpty() && phoneNumber.matches(telRegex)) {
				Map<String, String> request = new HashMap<String, String>();
				request.put("phone_number", phoneNumber);
				AsyncHttpUtilClient.doPost(
						AsyncHttpUtilClient.POST,
						MyApplication.url
								+ MyApplication.requestMap
										.get(MyApplication.USER_GETCODE), null,
						new RequestParams(request),
						new BooleanResponse<Boolean>() {

							@Override
							public void onSuccess(Boolean t) {
								// TODO Auto-generated method stub
								UIHandler.obtainMessage(TOASTMSG, "发送成功")
										.sendToTarget();
								hasSend = true;
								handler.sendEmptyMessage(1);
								handler.sendEmptyMessageDelayed(0, 1000);
							}

							@Override
							public void onFailure(int arg0, String arg1) {
								hasSend = false;
								handler.sendEmptyMessage(1);
								UIHandler.obtainMessage(TOASTMSG, arg1)
										.sendToTarget();
							}
						});
			}
			break;
		case R.id.register:

			code = authcode.getText().trim();
			phoneNumber = phone_number.getText().trim();
			passWord = password.getText().trim();
			userName = username.getText().trim();
			confirmpswd = iet_register_confirmpswd.getText().trim();
			if (userName.isEmpty()) {
				UIHandler.obtainMessage(TOASTMSG, "请填写用户名").sendToTarget();
				return;
			}
			if (code.isEmpty() || code.length() != 6) {
				UIHandler.obtainMessage(TOASTMSG, "请填写正确验证码").sendToTarget();
				return;
			}
			if (passWord.isEmpty() || !StrPattern.checkpswd(passWord)) {
				UIHandler.obtainMessage(TOASTMSG, "密码应为6-20位数字字母组合")
						.sendToTarget();
				return;
			}
			if (confirmpswd.isEmpty() || !StrPattern.checkpswd(confirmpswd)) {
				UIHandler.obtainMessage(TOASTMSG, "确认密码应为6-20位数字字母组合")
						.sendToTarget();
				return;
			}

			if (!confirmpswd.equals(passWord)) {
				UIHandler.obtainMessage(TOASTMSG, "密码不一致").sendToTarget();
				return;
			}
			Map<String, String> request = new HashMap<String, String>();
			request.put("phone_number", phoneNumber);
			request.put("code", code);
			request.put("password", passWord);
			request.put("nick_name", userName);

			AsyncHttpUtilClient.doPost(
					AsyncHttpUtilClient.POST,
					MyApplication.url
							+ MyApplication.requestMap
									.get(MyApplication.USER_REGISTER), null,
					new RequestParams(request), new BooleanResponse<Boolean>() {

						@Override
						public void onSuccess(Boolean t) {
							// TODO Auto-generated method stub
							UIHandler.obtainMessage(TOASTMSG, "注册成功")
									.sendToTarget();
							finish();
						}

						@Override
						public void onFailure(int arg0, String str) {
							UIHandler.obtainMessage(TOASTMSG, str)
									.sendToTarget();
						}
					});
			break;
		}
	}

}
